package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/6/26
 * @desc 演示接收请数据
 */
@WebServlet("/req")
public class Demo1Servlet extends HttpServlet {

    /**
     *  通过doGet方法,中的参数HttpServletRequest req这个对象
     *  提供很多方法,可以获得请求数据
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求前设置编码格式
        req.setCharacterEncoding("UTF-8");

        String uri = req.getRequestURI( );// 资源标识符
        System.out.println("uri = " + uri);

        StringBuffer url = req.getRequestURL( );// 资源定位符
        System.out.println("url = " + url);

        String method = req.getMethod( );// 请求方式get|post
        System.out.println("method = " + method);

        String protocol = req.getProtocol( );// 协议(了解)
        System.out.println("protocol = " + protocol);

        System.out.println("-----------------------" );
        // 各种请求头(不重要,了解)
        Enumeration<String> headerNames = req.getHeaderNames( );
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement( );
            String value = req.getHeader(headName);
            System.out.println(headName+ " : " + value);
        }
        System.out.println("-----------------------" );

        // 最重要的!!!!! 接收请求数据!!!!
        // 这个方法的参数,就是前端的name属性值
        // 明文,暗文,单选,下拉,日期等都是一个方法
        // 前端发送的数据,后端接收后都是字符串
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 字符串需要解析为数字
        String sex = req.getParameter("sex");
        int i = Integer.parseInt(sex);
        System.out.println(i == 1 ? "男":"女" );

        String email = req.getParameter("email");

        // 日期字符串需要解析为日期对象
        String birthday = req.getParameter("birthday");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(parse );

        String edu = req.getParameter("edu");

        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("sex = " + sex);
        System.out.println("email = " + email);
        System.out.println("birthday = " + birthday);
        System.out.println("edu = " + edu);

        // 复选框
        String[] skills = req.getParameterValues("skill");
        for (int i1 = 0; skills != null && i1 < skills.length; i1++) {
            System.out.println("skill = " + skills[i1]);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
